package xyf.p08.backtrack;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: P0039_CombinationSum
 * Description:
 * Author: xyf
 * Date: 9/8/22 4:46 PM
 * Version: 1.0
 **/
public class P0039_CombinationSum
{
    private List<List<Integer>> res = new ArrayList<>();

    public List<List<Integer>> combinationSum(int[] candidates, int target)
    {
        dfs(candidates, target, new ArrayList<>());
        return res;
    }

    private void dfs(int[] candidates, int target, List<Integer> path)
    {
        if (target == 0)
        {
            res.add(path);
        }
        if (target < 0)
        {
            return;
        }

        for (int i = 0; i < candidates.length; i++)
        {
            path.add(candidates[i]);
            dfs(candidates, target - candidates[i], path);
            path.remove(path.size() - 1);
        }
    }
}